/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.ajile.xmpp.test;

import java.io.BufferedReader;
import java.io.InputStreamReader;
import org.jivesoftware.smack.ConnectionConfiguration;
import org.jivesoftware.smack.PacketListener;
import org.jivesoftware.smack.XMPPConnection;
import org.jivesoftware.smack.packet.Message;
import org.jivesoftware.smack.packet.Packet;
import org.jivesoftware.smack.packet.Presence;
import org.jivesoftware.smack.provider.ProviderManager;
import org.jivesoftware.smackx.ServiceDiscoveryManager;

/**
 *
 * @author william.swaney
 */
public class SimpleExample {

    static String user = "ajiledemo7";
    static String pw = "aj1l3DEMO7";
    static String otherUser = "smack4cdc2";
    static String resource = "laptop";
    static String otherResource = "smack4cdc2";
    static XMPPConnection connection;

    public static void main(String[] args) throws Exception {
        System.out.println("SimpleExample.main()");

        ServiceDiscoveryManager.setIdentityName(resource);

        ProviderManager.getInstance();

        // connect to gtalk server
        //ConnectionConfiguration connConfig = new ConnectionConfiguration("talk.google.com", 5222, "gmail.com");
        ConnectionConfiguration connConfig = new ConnectionConfiguration("talk.google.com", 5222, "gmail.com");
        connConfig.setSASLAuthenticationEnabled(false);
        connection = new XMPPConnection(connConfig);
        connection.connect();

// login with username and password
        //connection.login("wswaney@ajile.com", "big00sh","1");
        connection.login(user, pw, resource);

        PacketListener pl = new PacketListener() {

            public void processPacket(Packet p) {
                //System.out.println(p.getFrom() + ": " + p.toString());
                if (p instanceof Message) {
                    Message msg = (Message) p;
                    System.out.println("in> [" + msg.getFrom() + "] " + msg.getBody());

                    Message msg2 = new Message();
                    msg2.setFrom(user + "@gmail.com/" + resource);
                    msg2.setTo(msg.getFrom());
                    msg2.setType(Message.Type.normal);
                    msg2.setBody("Reply: " + msg.getBody());
                    System.out.println("out> " + msg.toXML());
                    connection.sendPacket(msg);
                }
            }
        };
        //connection.addPacketListener(pl, null);

        Presence presence = new Presence(Presence.Type.available);
        presence.setFrom(user + "@gmail.com/" + resource);
        connection.sendPacket(presence);

        // Create the file transfer manager
        //final FileTransferManager manager = new FileTransferManager(connection);

        boolean sendFile = false;
        boolean zeroOrOne = false;
        /*if (sendFile) {
        // Create the outgoing file transfer
        System.out.println("about to send transfer");
        OutgoingFileTransfer transfer = manager.createOutgoingFileTransfer("wswaney@ajile.com");
        transfer.setResponseTimeout(10000);

        transfer.getAmountWritten();

        // Send the file
        if (zeroOrOne) {
        transfer.sendFile(new File("/SmackSendFile0.jpg"), "You won't believe this!");
        } else {
        transfer.sendFile(new File("/SmackSendFile1.jpg"), "You won't believe this!");
        }
        zeroOrOne = !zeroOrOne;

        while (true) {
        System.out.println("transfer.getAmountWritten()" + transfer.getAmountWritten());
        Thread.sleep(500);
        if (transfer.isDone()) {
        System.out.println("transfer is done, error? " + transfer.getError());
        break;
        }
        }

        } /*else {
        // Create the listener

        manager.addFileTransferListener(new FileTransferListener() {

        int count = 1;

        public void fileTransferRequest(FileTransferRequest request) {
        // Check to see if the request should be accepted
        boolean accept = true;
        //int count = 1;
        if (accept) {
        // Accept it
        IncomingFileTransfer transfer = request.accept();
        System.out.println("fileTransferRequest() accepted " + (count - 1));
        try {
        //count++;
        //transfer.recieveFile(new File("/SmackFile" + (count - 1) + ".jpg"));
        transfer.recieveFile();//inputstream
        //transfer.cancel();
        count++;
        } catch (XMPPException ex) {
        ex.printStackTrace();
        }
        } else {
        // Reject it
        System.out.println("fileTransferRequest() rejected");
        request.reject();
        }
        }
        });
        }*/


        System.out.println("connection.isConnected() " + connection.isConnected());
        System.out.println("connection.isAuthenticated() " + connection.isAuthenticated());
        System.out.println("connection.getUser() " + connection.getUser());

        Message monitor = new Message();
        //monitor.setActive(true);
        monitor.setTo("ajiledemo7@gmail.com/android");
        monitor.setFrom("ajiledemo3@gmail.com/android");
        connection.sendPacket(monitor);



        BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
        String line = null;
        while (!(line = reader.readLine()).equals("exit")) {
            //System.out.println("out> " + line);
            Message msg = new Message();
            msg.setFrom(user + "@gmail.com/" + resource);
            msg.setTo(otherUser + "@gmail.com/" + otherResource);
            msg.setType(Message.Type.normal);
            msg.setBody(line);
            System.out.println("out> " + msg.toXML());
            connection.sendPacket(msg);

        }

        connection.disconnect();

    }
}
